import {createRouter, createWebHistory} from 'vue-router'
import '../assets/css/global.css'
import store from '../store'
import login from '../api/login'

// 路由列表
const routes = [
  {
    meta: {
      title: '首页',
      keepalive: true, // 是否缓存页面,
    },
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue')
  },

  {
    meta: {
      title: '登录',
      keepalive: false, // 是否缓存页面,
    },
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },


  {
    meta: {
      title: '个人中心',
      keepalive: false, // 是否缓存页面,
      authorization: true // 是否需要登录验证
    },
    path: '/user',
    name: 'User',
    component: () => import('../views/User.vue')
  },

  {
    meta: {
      title: "发表文章",
      keepalive: false, // 是否缓存页面,
      authorization: true // 是否需要登录验证
    },
    path: '/articles',
    name: 'Articles',
    component: () => import('../views/Articles.vue')
  },

  {
    meta: {
      title: "所有文章",
      keepalive: true, // 是否缓存页面,
      authorization: true // 是否需要登录验证
    },
    path: '/data',
    name: 'Data',
    component: () => import('../views/Data.vue')
  }

]

// 路由对象实例化
const router = createRouter({
  // history, 指定路由的模式
  history: createWebHistory(),
  // 路由列表
  routes,
});

// 路由全局前置守卫
router.beforeEach((to, from, next)=>{
  document.title=to.meta.title
  // 登录状态验证
  if (to.meta.authorization && !store.getters.getUserInfo) {
    next({"name": "Login"})
    login.is_register = false
  }else{
    next()
  }
})

// 暴露路由对象
export default router